<template>
  <el-dialog
    :visible.sync="dialogVisible"
    :title="approveType==='pass'?'通过申请':'拒绝申请'"
    width="600px"
    :close-on-click-modal="false"
    @close="close"
  >
    <el-form ref="form" v-loading="loading" :model="form" label-width="100px">
      <el-form-item v-if="type==='single'&&row" label="申请人：">{{ row.name }}&nbsp;&nbsp;{{ row.phone }}</el-form-item>

      <el-form-item v-if="type==='single'&&row" label="申请门店：">{{ row.shopName }}</el-form-item>

      <el-form-item v-if="approveType==='pass'" label="选择角色：">
        <el-select
          v-model="form.roles"
          value-key="id"
          style="width: 100%"
          multiple
          placeholder="请选择"
        >
          <el-option v-for="item in roles" :key="item.id" :label="item.name" :value="item" />
        </el-select>
      </el-form-item>

      <el-form-item v-if="approveType==='refuse'" label="备注：" prop="remark">
        <el-input v-model="form.remark" maxlength="30" placeholder="请输入拒绝理由" />
      </el-form-item>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button size="small" round @click="close">取 消</el-button>
      <el-button size="small" round type="primary" :disabled="loading" @click="submit()">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { mapGetters } from 'vuex'
// import { validateStrLen } from '@/utils/validate.js'
import { approveUser } from '@/api/user'
import { allRole } from '@/api/role'

export default {
  props: {
    scope: {
      require: true,
      type: Number
    }
  },

  data() {
    return {
      title: null,
      resolve: null,
      reject: null,
      // 状态
      loading: false,
      dialogVisible: null,
      // 数据
      approveType: 'pass',
      type: '',
      userIds: [],
      row: '',
      roles: [],
      form: {
        approveIds: [],
        status: '',
        roles: [],
        remark: ''
      }
    }
  },
  computed: {
    ...mapGetters(['userSuffix'])
  },
  methods: {
    pass(type, data) {
      this.approveType = 'pass'
      this.open(type, data)
      return new Promise((resolve, reject) => {
        this.resolve = resolve
        this.reject = reject
      })
    },

    refuse(type, data) {
      this.approveType = 'refuse'
      this.open(type, data)
      return new Promise((resolve, reject) => {
        this.resolve = resolve
        this.reject = reject
      })
    },

    open(type, data) {
      this.type = type
      if (type === 'batch') {
        this.userIds = data
      } else {
        this.row = data
      }
      // 拉取所有的角色
      allRole(this.scope)
        .then(res => {
          this.roles = res.data
        })
        .catch(() => {})
      this.show()
    },

    close() {
      this.loading = false
      this.approveType = 'pass'
      this.type = ''
      this.userIds = []
      this.row = ''
      this.roles = []
      this.form = {
        approveIds: [],
        status: '',
        roles: [],
        remark: ''
      }
      this.resolve = null
      this.reject = null
      this.dialogVisible = false
    },

    show() {
      this.dialogVisible = true
    },

    // 提交
    submit() {
      const data = Object.assign({}, this.form)
      if (this.approveType === 'pass') {
        data.status = 1
      } else {
        data.status = 2
      }
      if (this.type === 'batch') {
        data.approveIds = this.userIds
      } else {
        data.approveIds = [this.row.id]
      }
      this.loading = true
      approveUser(data)
        .then(res => {
          this.$notify({
            title: '成功',
            message: data.status === 1 ? '审核成功' : '已拒接',
            type: 'success',
            duration: 2000,
            offset: 40
          })
          this.resolve && this.resolve()
          this.close()
        })
        .catch(() => {})
        .finally(() => {
          this.loading = false
        })
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
  .el-select {
    width: 100%;
  }
</style>
